package com.biz.primus.app.rest.controller.member;

import com.biz.primus.app.rest.controller.base.RestBaseController;
import com.biz.primus.app.rest.feign.member.MemberFeignClient;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.model.member.exception.UserExceptionResponse;
import com.biz.primus.model.member.exception.UserExceptionType;
import com.biz.primus.model.member.vo.BaseVo;
import com.biz.primus.model.member.vo.MemberAddressLabelVo;
import com.biz.primus.model.member.vo.MemberAddressVo;
import com.biz.primus.model.member.vo.request.MemberAddressCheckLabelNameRequestVo;
import com.biz.primus.model.member.vo.request.MemberAddressIdRequestVo;
import com.biz.primus.model.member.vo.request.MemberAddressNewRequestVo;
import com.biz.primus.model.member.vo.response.MemberAdddressCheckStoreResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收货地址相关Controller
 *
 * @author jun.liu
 * @usage
 * @reviewer
 * @since 2017年11月3日
 */
@RestController
@RequestMapping("/users/address/new")
@Api(value = "用户收货地址模块接口文档")
public class AddressController extends RestBaseController
{

    private final MemberFeignClient memberFeignClient;

    @Autowired
    public AddressController(MemberFeignClient memberFeignClient) {
        this.memberFeignClient = memberFeignClient;
    }

    @ApiOperation(value = "新建或保存标签", response = MemberAddressLabelVo.class, responseContainer = "list")
    @PostMapping("/updateOrSaveLabel")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.ID_ERROR,
            UserExceptionType.USER_NOT_EXIST,UserExceptionType.ADDRESS_LABEL_REPEAT
    })
    public JsonResult saveOrUpdateLabel(GlobalParams gp, @RequestBody MemberAddressCheckLabelNameRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        // 保存标签，重复时会抛出异常
        memberFeignClient.saveOrUpdateLabel(reqVo);
        // 获取所有标签
        List<MemberAddressLabelVo> respVo = memberFeignClient.getAllUserLabel(reqVo.getMemberId());
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "获取所有的标签名称（用于在：用户新增地址时，显示在出来的标签名字）", response = MemberAddressLabelVo.class, responseContainer = "list")
    @GetMapping("/allLabels")
    public JsonResult getAllUserLabel(GlobalParams gp)
    {
        MemberAddressNewRequestVo reqVo = buildRequest(gp, new MemberAddressNewRequestVo());
        List<MemberAddressLabelVo> resp = memberFeignClient.getAllUserLabel(reqVo.getMemberId());
        return new JsonResult(resp);
    }

    @ApiOperation(value = "删除标签", response = MemberAddressLabelVo.class, responseContainer = "list")
    @PostMapping("/delLabel")
    @UserExceptionResponse({
            UserExceptionType.LABEL_NOT_EXIST,UserExceptionType.HOME_COMPANY_CANT_DEL
    })
    public JsonResult deleteLabelNew(GlobalParams gp, @RequestBody MemberAddressIdRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        List<MemberAddressLabelVo> resp = memberFeignClient.deleteLabelNew(reqVo);
        return new JsonResult(resp);
    }

    @ApiOperation(value = "保存或者更新地址", response = MemberAddressVo.class, responseContainer = "list")
    @PostMapping("/saveOrUpdate")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR
    })
    public JsonResult saveOrUpdateAddress(GlobalParams gp, @RequestBody MemberAddressVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        List<MemberAddressVo> resp = memberFeignClient.saveOrUpdateAddress(reqVo);
        return new JsonResult(resp);
    }

    @ApiOperation(value = "删除收货地址", response = MemberAddressVo.class, responseContainer = "list")
    @PostMapping("/delAddress")
    public JsonResult deleteAddressNew(GlobalParams gp,  @RequestBody MemberAddressIdRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        List<MemberAddressVo> respVo = memberFeignClient.deleteAddressNew(reqVo);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "获取所有的收货地址", response = MemberAddressVo.class, responseContainer = "list")
    @GetMapping("/getAllAddress")
    public JsonResult getAllAddressNew(GlobalParams gp)
    {
        MemberAddressNewRequestVo reqVo = buildRequest(gp, new MemberAddressNewRequestVo());
        reqVo.setLon(null);
        reqVo.setLat(null);
        List<MemberAddressVo> resp = memberFeignClient.getAllAddress(reqVo);
        return new JsonResult(resp);
    }

    @ApiOperation(value = "查看收货地址附近有无门店", response = MemberAdddressCheckStoreResponseVo.class)
    @GetMapping("/checkStore")
    public JsonResult checkStore(GlobalParams gp, @RequestBody BaseVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        MemberAdddressCheckStoreResponseVo resp = memberFeignClient.checkStore(reqVo);
        return new JsonResult(resp);
    }
}
